//
//  PEChargeDataCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/12.
//  参考:https://www.cnblogs.com/wangkejia/archive/2021/11/01/15494841.html

#import "RHChargeDataCell.h"
#import "RHChargeBarChartView.h"

@interface RHChargeDataCell()

@property (nonatomic ,strong) RHChargeBarChartView *barChartView;

@end
@implementation RHChargeDataCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    QMUILabel *leftLab = [self.contentView viewWithTag:100];
    leftLab.text = [NSString stringWithFormat:@"%ldkwh",[dic[@"accumulatedCharge"] integerValue]];
    
    QMUILabel *rightLab = [self.contentView viewWithTag:101];
    rightLab.text = [NSString stringWithFormat:@"%ldkwh",[dic[@"accumulatedDischarge"] integerValue]];
}

- (void)setChartModel:(DevChartModel *)chartModel{
    _chartModel = chartModel;

    [_barChartView setChartModel:_chartModel];
}

- (void)setupUI{
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    kWeakSelf;
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = CFTabViewBgColor;
    [bgView.layer setCornerRadius:8];
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    //------------------------
    UIView *top_bgView = kView;
    top_bgView.backgroundColor = CFTabViewBgColor;
    [bgView addSubview:top_bgView];
    NSMutableArray *itemArr = [NSMutableArray array];
    NSArray *titleArr = @[@"累计总充电量",@"累计总放电量"];
    [titleArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *item_bgView = kView;
        item_bgView.backgroundColor = [UIColor whiteColor];
        [item_bgView.layer setCornerRadius:8];
        item_bgView.layer.masksToBounds = YES;
        [top_bgView addSubview:item_bgView];
        [itemArr addObject:item_bgView];
        [item_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_offset(0);
        }];
        
        NSString *iconStr = [NSString stringWithFormat:@"charge0%zd",idx+1];
        UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:iconStr]];
        [item_bgView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(10);
            make.width.height.offset(30);
            make.centerY.mas_offset(0);
        }];
        
        
        QMUILabel *titleLab = [[QMUILabel alloc]init];
        titleLab.text = titleArr[idx];
        titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
        titleLab.textAlignment = NSTextAlignmentLeft;
        titleLab.font = [UIFont systemFontOfSize:13];
        [item_bgView addSubview:titleLab];
      
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(icon.mas_right).offset(10);
            make.right.mas_offset(0);
            make.bottom.mas_offset(-10);
        }];
        
      
        
        QMUILabel *contentLab = [[QMUILabel alloc]init];
        contentLab.tag = 100 + idx;
        contentLab.text = [NSString stringWithFormat:@"%dkWh",arc4random_uniform(30)];
        contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
        contentLab.textAlignment = NSTextAlignmentLeft;
        contentLab.font = [UIFont boldSystemFontOfSize:16];
        [item_bgView addSubview:contentLab];
       
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(icon.mas_right).offset(10);
            make.top.mas_offset(10);
        }];
    }];
    
    [top_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.top.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(70);
    }];
    
    [itemArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
    
    
    
    //柱状图
    _barChartView = [[RHChargeBarChartView alloc]initWithFrame:CGRectMake(0, 0, 200, 200) type:0];
    _barChartView.backgroundColor = UIColor.whiteColor;
    _barChartView.layer.cornerRadius = 8;
    _barChartView.layer.masksToBounds = YES;
    [bgView addSubview:_barChartView];
    [_barChartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.equalTo(top_bgView.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    _barChartView.fullChartBlock = ^{
        if (weakSelf.fullChartBlock) {
            weakSelf.fullChartBlock();
        }
    };
    _barChartView.selectTopToolBlock = ^(NSInteger index) {
        if (weakSelf.selectTopToolBlock) {
            weakSelf.selectTopToolBlock(index);
        }
    };
    
    _barChartView.subTimeBlock = ^{//日期往前
        if (weakSelf.subTimeBlock) {
            weakSelf.subTimeBlock();
        }
    };
    
    _barChartView.addTimeBlock = ^{//日期往后
        if (weakSelf.addTimeBlock) {
            weakSelf.addTimeBlock();
        }
    };
    
    _barChartView.changeTimeBlock = ^{
        if (weakSelf.changeTimeBlock) {
            weakSelf.changeTimeBlock();
        }
    };
}









@end
